<?php

namespace App\Http\Controllers\Backstage\Article;

use App\Models\Article;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\URL;
use Illuminate\Http\Request;
use Fukuball\Jieba\Jieba;
use Fukuball\Jieba\Finalseg;
//文章管理

class ArticleController extends Controller
{
    //文章列表
    public function articlemanage(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }

        //翻页
        $pagenNum = (int) $request->query('page');
        $pageSize = (int) $request->query('pageSize');
        if ($pageSize > 50) {
            return false;
        }
        $page = $pagenNum - 1;
        if ($page != 0) {
            $page = $pageSize * $page;
        }
        //联表查询
        $count = Article::leftjoin('upload_file', 'article.fileid', '=', 'upload_file.fileid')->count();
        $info = Article::leftjoin('upload_file', 'article.fileid', '=', 'upload_file.fileid')
            ->leftJoin('article_cate', 'article.catid', '=', 'article_cate.catid')
            ->orderByDesc("article_create_time")
            // ->join('orders', 'users.id', '=', 'orders.user_id')
            // ->select('users.*', 'contacts.phone', 'orders.price')
            // ->paginate(15);
            ->offset($page)
            ->limit($pageSize)
            ->get();
        // $data['data'] = $info;
        // return $data;
        if ($info) {
            $info = json_decode($info, true); //对象转成数组
            foreach ($info as $key => $value) {
                $info[$key]['file_name'] =  http_type() . $_SERVER['HTTP_HOST'] . '/uploads/images/' . date('Y-m-d', $value['create_flie_time']) . "/" . $value['file_name'];
                $info[$key]['article_create_time'] = date('Y-m-d', $value['article_create_time']);
            }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        $data['count'] = $count;
        return $data;
    }

    //获取文章详情
    public function getArticleDetail(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['article.aid'] = (int) $request->query('aid');
        // $aid = $request->query('aid');
        $info = Article::leftjoin('article_cate', 'article.catid', '=', 'article_cate.catid')
            ->leftjoin('upload_file', 'article.fileid', '=', 'upload_file.fileid')
            ->select('article.aid', 'article_cate.catid', 'article.article_title', 'article.article_author', 'article.article_introduction', 'article.article_content', 'article_cate.article_cate_title', 'upload_file.fileid', 'upload_file.file_name', 'upload_file.create_flie_time')
            ->where($map)
            ->first();
        if ($info) {
            $info->url =  http_type() . $_SERVER['HTTP_HOST'] . '/uploads/images/' . date('Y-m-d', $info->create_flie_time) . "/" . $info->file_name;
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        return $data;
    }


    //添加文章

    public function addarticle(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $data['article_title'] = $request->input('title');
        $data['catid'] = (int) $request->input('catid');
        $data['fileid'] = (int) $request->input('fileid');
        $data['article_introduction'] = $request->input('introduction');
        $data['article_content'] = $request->input('content');
        $data['article_create_time'] = time();
        $info = Article::insert($data);
        if ($info) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }



    //修改文章
    public function updatearticle(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $map['aid'] = (int) $request->input('aid');
        $data['article_title'] = $request->input('title');
        $data['catid'] = (int) $request->input('catid');
        $data['fileid'] = (int) $request->input('fileid');
        $data['article_introduction'] = $request->input('introduction');
        $data['article_content'] = $request->input('content');
        $info = Article::where($map)->update($data);
        if ($info) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }

    //删除文章
    public function delarticle(Request $request)
    {
        $token = $request->query('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $data['aid'] = (int)$request->input('aid');
        $info = Article::where($data)->delete();
        if ($info) {
            $data = success();
        } else {
            $data = fail();
        }
        return $data;
    }


    //搜索文章
    public function Searcharticle(Request $request)
    {
        $token = $request->input('token');
        //验证权限
        $verifytoken = verifyBackstageToken($token);
        if (is_array($verifytoken)) {
            return $verifytoken;
        }
        $title = $request->input('title');
        // $info = DB::table('article')->get();
        // Article::all()->toArray();
        // dump(Article::search('功能齐全的搜索引擎')->get()->toArray());


        $page = (int) $request->input('page');
        $pageSize = (int) $request->input('pageSize');
        //翻页
        $page = $page - 1;
        if ($page != 0) {
            $page = $pageSize * $page;
            $pageSize = $pageSize * $page;
        }
        $info = Article::search($title)
        ->paginate($page);
        // ->with('haManyUploadFile')->get();
        return response()->json($info);//转化为json数据
        //联表查询
        // $count = Article::search($title)
        //     ->join('upload_file', 'article.fileid', '=', 'upload_file.fileid')
        //     ->count();

        // $info = Article::search($title)
        //     ->join('upload_file', 'article.fileid', '=', 'upload_file.fileid')
        //     ->offset($page)
        //     ->limit($pageSize)
        //     ->get();
        // $info=DB::table('article')->where('title', 'like', '%' . $title . '%')->get();
        if ($info) {
            // $info = json_decode($info, true); //对象转成数组
            // foreach ($info as $key => $value) {
            //     $info[$key]['file_name'] = http_type() . $_SERVER['HTTP_HOST'] . '/uploads/images/' . date('Y-m-d', $value['create_flie_time']) . "/" . $value['file_name'];
            //     $info[$key]['article_create_time'] = date('Y-m-d', $value['article_create_time']);
            // }
            $data = success();
            $data['data'] = $info;
        } else {
            $data = fail();
        }
        // $data['count'] = $count;
        return $data;
    }
    //文章搜索
    public function fenci()
    {
        ini_set('memory_limit', '512M');
        Jieba::init();
        Finalseg::init();
        $info = DB::table('article')->get();
        $info = json_decode($info, true);
        foreach ($info as $key => $value) {
            $date['article_title_index'] = Jieba::cut($value['article_title']);
            $map['aid'] = $value['aid'];
            DB::table('article')->where($map)->update($date);
            $msg[$key] = $key;
        }

        return $msg;
    }
}
